using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Net.Fan.SmallPig.DataBase;

namespace Net.Fan.SmallPig.DataAccess
{
    /// <summary>
    /// 
    /// </summary>
    public class MessageDA
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static DataTable GetUnreadMessages(string userName)
        {
            string sql = "SELECT * FROM MsgReceiver r,Messages m where (Readed is null or Readed='False') and (Recycle is null or Recycle='False') and (Deleted='False' or Deleted is null) and Receiver=@Receiver and r.MessageID=m.MessageID order by SendTime desc";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@Receiver", userName);

            return DA_Base.RunReturnDataset(sql, para).Tables[0];
        }

        public static int GetUnreadAmount(string userName)
        {
            string sql = "SELECT count(*) FROM MsgReceiver where (Readed is null or Readed='False') and (Recycle is null or Recycle='False') and (Deleted='False' or Deleted is null) and Receiver=@Receiver";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@Receiver", userName);

            return (int)DA_Base.ExecuteScalar(sql, para);
        }

        public static DataTable GetReadedMessages(string userName)
        {
            string sql = "SELECT * FROM MsgReceiver r,Messages m where Readed='True' and (Recycle='False' or Recycle is null) and Receiver=@Receiver and r.MessageID=m.MessageID order by SendTime desc";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@Receiver", userName);

            return DA_Base.RunReturnDataset(sql, para).Tables[0];
        }

        public static DataTable GetRecycleMessages(string userName)
        {
            string sql = "SELECT * FROM MsgReceiver r,Messages m where Recycle='True' and (Deleted='False' or Deleted is null) and Receiver=@Receiver and r.MessageID=m.MessageID order by SendTime desc";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@Receiver", userName);

            return DA_Base.RunReturnDataset(sql, para).Tables[0];
        }

        public static DataTable GetInputBoxMessages(string userName)
        {
            string sql = "SELECT * FROM MsgReceiver r,Messages m where (Recycle='False' or Recycle is null) and (Deleted='False' or Deleted is null) and Receiver=@Receiver and r.MessageID=m.MessageID order by SendTime desc";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@Receiver", userName);

            return DA_Base.RunReturnDataset(sql, para).Tables[0];
        }

        public static DataRow GetMessage(long id, string userName)
        {
            string sql = "SELECT * FROM MsgReceiver r,Messages m where r.MessageID=@ID and r.MessageID=M.MessageID and Receiver=@Receiver";
            SqlParameter[] para = new SqlParameter[2];
            para[0] = new SqlParameter("@ID", id);
            para[1] = new SqlParameter("@Receiver", userName);

            DataSet ds = DA_Base.RunReturnDataset(sql, para);

            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0];
            }
            else
            {
                return null;
            }
        }

        public static DataTable GetReceivers(long msgId)
        {
            string sql = "SELECT * FROM MsgReceiver where MessageID=@ID";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@ID", msgId);
            DataSet ds = DA_Base.RunReturnDataset(sql, para);
            return ds.Tables[0];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="title"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static long Insert(string sender, string title, string body)
        {
            string sql = "INSERT INTO Messages (Sender,Title,Body,SendTime) VALUES (@Sender,@Title,@Body,GetDate())";
            sql += " select @@identity";

            SqlParameter[] para = new SqlParameter[3];
            para[0] = new SqlParameter("@Body", body);
            para[1] = new SqlParameter("@Sender", sender);
            para[2] = new SqlParameter("@Title", title);

            decimal dd = (decimal)DA_Base.ExecuteScalar(sql, para);
            return Convert.ToInt64(dd);
        }

        public static int InsertReceiver(string receiver, long messageId)
        {
            string sql = "INSERT INTO MsgReceiver (Receiver,MessageId) VALUES (@Receiver,@MessageId)";
            SqlParameter[] para = new SqlParameter[2];
            para[0] = new SqlParameter("@Receiver", receiver);
            para[1] = new SqlParameter("@MessageId", messageId);
            return DA_Base.ExecuteNonQuery(sql, para);
        }

        public static int SetReadFlag(long id, string userName)
        {
            string sql = "update msgReceiver set Readed=1 where MessageID=@ID and Receiver=@Receiver";
            SqlParameter[] para = new SqlParameter[2];
            para[0] = new SqlParameter("@ID", id);
            para[1] = new SqlParameter("@Receiver", userName);
            return DA_Base.ExecuteNonQuery(sql, para);
        }

        public static int SetDeletedFlag(long id, string userName)
        {
            string sql = "update MsgReceiver set Deleted=1 where MessageID=@ID and Receiver=@Receiver";
            SqlParameter[] para = new SqlParameter[2];
            para[0] = new SqlParameter("@ID", id);
            para[1] = new SqlParameter("@Receiver", userName);
            return DA_Base.ExecuteNonQuery(sql, para);
        }

        public static int TrueDelete(long id)
        {
            string sql = "delete MsgReceiver where MessageID=@ID";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@ID", id);
            DA_Base.ExecuteNonQuery(sql, para);

            sql = "delete Messages where MessageID=@ID";
            para = new SqlParameter[1];
            para[0] = new SqlParameter("@ID", id);
            return DA_Base.ExecuteNonQuery(sql, para);
        }

        public static int GetNoDeleteCount(long id)
        {
            string sql = "select count(*) from MsgReceiver where MessageID=@ID and (Deleted='False' or Deleted is null)";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@ID", id);
            object o = DA_Base.ExecuteScalar(sql, para);
            return (int)o;
        }

        public static int DeleteMsgReceivers(long id)
        {
            string sql = "delete MsgReceiver where MessageID=@ID";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@ID", id);
            return DA_Base.ExecuteNonQuery(sql, para);
        }

        public static int Move2RecycleBin(long id, string userName)
        {
            string sql = "update MsgReceiver set Recycle=1 where MessageID=@ID and Receiver=@UserName";
            SqlParameter[] para = new SqlParameter[2];
            para[0] = new SqlParameter("@ID", id);
            para[1] = new SqlParameter("@UserName", userName);
            return DA_Base.ExecuteNonQuery(sql, para);
        }

        public static int RecycleMessage(long id, string userName)
        {
            string sql = "update MsgReceiver set Recycle='False' where MessageID=@ID and Receiver=@UserName";
            SqlParameter[] para = new SqlParameter[2];
            para[0] = new SqlParameter("@ID", id);
            para[1] = new SqlParameter("@UserName", userName);
            return DA_Base.ExecuteNonQuery(sql, para);
        }

        public static long GetNextMessage(long old, string userName, bool forward)
        {
            string sql = "select top 1 MessageID from  MsgReceiver where (Recycle='False' or Recycle is null) and (Readed is null or Readed='False') and Receiver=@UserName";

            if (forward)
            {
                sql = sql + " and MessageID<@ID";

            }
            else
            {
                sql = sql + " and MessageID>@ID";
            }

            if (forward)
            {
                sql = sql + " order by MessageID";
            }
            else
            {
                sql = sql + " order by MessageID desc";
            }

            SqlParameter[] para = new SqlParameter[2];
            para[0] = new SqlParameter("@ID", old);
            para[1] = new SqlParameter("@UserName", userName);
            object o = DA_Base.ExecuteScalar(sql, para);
            if (o == null)
            {
                return (long)-1;
            }
            else
            {
                return (long)o;
            }
        }
    }
}
